using System;
using System.Web;
using System.Xml.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.WebControls;

namespace CKS.EnhancedContentQueryWebPart
{
    public class ConentQueryWithContextMenusToolPart : ToolPart
    {
        #region controls
        ConentQueryWithContextMenus webpart;
        CheckBox showToolbar;
        CheckBox showContextMenus;
        CheckBox showErrors;
        TextBox commonViewFields;
        TextBox dataSourcesString;
        TextBox itemXslLink;
        TextBox headerXslLink;
        TextBox mainXslLink;
        TextBox dataSourceID;
        Panel toolPanel;
        Table toolPanelTable;
        #endregion

        #region toolpane events
        public ConentQueryWithContextMenusToolPart()
        {
            this.Title = "Enhanced Settings";
        }
        protected override void CreateChildControls()
        {

            webpart = this.WebPartToEdit as ConentQueryWithContextMenus;
            toolPanel = new Panel();
            toolPanel.CssClass = "ms-ToolPartSpacing";
            toolPanel.Controls.Add(GetToolPanel());

            this.Controls.Add(toolPanel);
            base.CreateChildControls();

        }
        public override void ApplyChanges()
        {
            try
            {
                webpart.ShowContextMenus = showContextMenus.Checked;
                webpart.ShowToolbar = showToolbar.Checked;
                webpart.ShowErrors = showErrors.Checked;
                webpart.CommonViewFields = commonViewFields.Text;
                webpart.DataSourcesString = dataSourcesString.Text;
                webpart.ItemXslLink = itemXslLink.Text;
                webpart.HeaderXslLink = headerXslLink.Text;
                webpart.MainXslLink = mainXslLink.Text;
                webpart.DataSourceID = dataSourceID.Text;
            }
            catch { }
            base.ApplyChanges();
        }
        #endregion

        #region ToolPanel building functions
        private Control GetToolPanel()
        {
            toolPanelTable = new Table();
            toolPanelTable.CellPadding = 0;
            toolPanelTable.CellSpacing = 0;
            toolPanelTable.Style["border-collapse"] = "collapse";
            toolPanelTable.Attributes.Add("width", "100%");

            toolPanelTable.Rows.Add(GetShowContextMenusSettingsRow());
            toolPanelTable.Rows.Add(GetCommonViewFieldsRow());
            toolPanelTable.Rows.Add(GetDataSourcesStringRow());
            toolPanelTable.Rows.Add(GetDataSourceIDStringRow());
            toolPanelTable.Rows.Add(GetMainXslLinkRow());
            toolPanelTable.Rows.Add(GetItemXslLinkRow());
            toolPanelTable.Rows.Add(GetHeaderXslLinkRow());
            toolPanelTable.Rows.Add(GetSeperatorRow());
            toolPanelTable.Rows.Add(GetShowToolBarSettingsRow());
            toolPanelTable.Rows.Add(GetSeperatorRow());
            toolPanelTable.Rows.Add(GetDebuggingSettingsRow());
            toolPanelTable.Rows.Add(GetSeperatorRow());

            return toolPanelTable;
        }
        private TableRow GetShowContextMenusSettingsRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Context Menus Settings:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            showContextMenus = new CheckBox();
            showContextMenus.Checked = this.webpart.ShowContextMenus;
            showContextMenus.Text = "Show Context Menus for Items?<br/>(Requires an xslt that can support it)";
            cell.Controls.Add(showContextMenus);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetShowToolBarSettingsRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Tool Bar Settings:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            showToolbar = new CheckBox();
            showToolbar.Checked = this.webpart.ShowToolbar;
            showToolbar.Text = "Show Tool Bar?<br/>(only in single document library mode)";
            cell.Controls.Add(showToolbar);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetDebuggingSettingsRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Debugging Settings:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            showErrors = new CheckBox();
            showErrors.Checked = this.webpart.ShowErrors;
            showErrors.Text = "Show Errors?";
            cell.Controls.Add(showErrors);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetCommonViewFieldsRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Common View Fields:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            commonViewFields = new TextBox();
            commonViewFields.Text = this.webpart.CommonViewFields;
            cell.Controls.Add(commonViewFields);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetDataSourcesStringRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Data Source String:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            dataSourcesString = new TextBox();
            dataSourcesString.Text = this.webpart.DataSourcesString;
            cell.Controls.Add(dataSourcesString);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetDataSourceIDStringRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Data Source String:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            dataSourceID = new TextBox();
            dataSourceID.Text = this.webpart.DataSourceID;
            cell.Controls.Add(dataSourceID);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetItemXslLinkRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Item XSL Link:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            itemXslLink = new TextBox();
            itemXslLink.Text = this.webpart.ItemXslLink;
            cell.Controls.Add(itemXslLink);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetHeaderXslLinkRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Header XSL Link:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            headerXslLink = new TextBox();
            headerXslLink.Text = this.webpart.HeaderXslLink;
            cell.Controls.Add(headerXslLink);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetMainXslLinkRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\"><b>Main XSL Link:</b></div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            mainXslLink = new TextBox();
            mainXslLink.Text = this.webpart.MainXslLink;
            cell.Controls.Add(mainXslLink);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));

            row.Cells.Add(cell);
            return row;
        }
        private TableRow GetSeperatorRow()
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div style='width:100%' class='UserDottedLine'></div>"));
            row.Cells.Add(cell);
            return row;
        }
        #endregion

        
    }
}
